﻿using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;

namespace SVMPluginViewer
{
    public partial class SVMViewerUserControl : UserControl
    {
        private ToolTip toolTip = new ToolTip();        // declare ToolTip object
        //private DataView dataviewTrainingset;
        //private bool bDataViewTrainingset_Intialized = false;        // flag whether the dataview 'dataviewTrainingset' has been initialized.

        // Store the position of the datapoint from the ScatterPlot in the datatable 'trainingset'
        // This position is needed for showing the ToolTip of the datapoint
        private void StoreDataPointCoordinates(DataRow myRow, double x, double y)
        {
            /*try
            {
                SetValue(myRow, "__x_coordinate", (int) x);
                SetValue(myRow, "__y_coordinate", (int) y);
            }
            catch
            {
                //MessageBox.Show("An execption occured in function 'StoreDataPointCoordinates'"); 
            }*/
        }


        // Setup DataView to search datapoint in datatable 'trainingset'
        // This search function is used for showing the Tooptip at the datapoint in the scatterplot.
        private void SetupDataViewTrainingset()
        {
            // initialize dataview
            //this.dataviewTrainingset = new DataView(this.trainingset);

            // set flag that the dataview has been intialized.
            //this.bDataViewTrainingset_Intialized = true;

        }



        // Search in the dataview 'dataviewTRainingset' whether a datapoint exist with the given x_coordinate and y_coordinate
        private bool DataPointExist(int x_coordinate, int y_coordinate)
        {
            ///string queryString = String.Format("__x_coordinate = '{0}' AND __y_coordinate = '{1}'", x_coordinate.ToString(), y_coordinate.ToString());
            int radius = 3;  // in pixels
            int x_min = x_coordinate - radius;
            int x_max = x_coordinate + radius;
            int y_min = y_coordinate - radius;
            int y_max = y_coordinate + radius;

            string queryString = String.Format("__x_coordinate >= '{0}' AND __x_coordinate <= '{1}' AND __y_coordinate >= '{2}' AND __y_coordinate <= '{3}'", x_min.ToString(), x_max.ToString(), y_min.ToString(), y_max.ToString());
            // to increate the sensitivity for viewing the tooltip, we introduce a 'radius' (area) around the datapoint, otherwise the tooltip will only be shown if the mouse pointer is pointed exactly on the datapoint. 


            try
            {               
                this.dataviewTrainingset.RowFilter = queryString;
            }
            catch
            {
                MessageBox.Show("An exeption occured in function 'DataPointExist'. The program needs to be terminated");
                Application.Exit();
            }

            if (this.dataviewTrainingset.Count > 0)
                return true;
            else
                return false;

        }



        private void MouseToolTip(MouseEventArgs e)
        {
            int x = e.X; 
            int y = e.Y;

            string x_value = "";
            string y_value = "";
            string class_value = "";
            string predict_value = "";
            string toolTipText = "";

            // Hide ToolTip
            this.toolTip.Hide(this);

            // First verify whether the dataview 'dataviewTrainingset has been initialized!.
            if (this.bDataViewTrainingset_Intialized)
            {   

                try
                {
                    // Check whether the mouse pointer is pointing at a datapoint in the scatter plot
                    if (DataPointExist(x, y))
                    {
                        // retrieve values from the dataview 'dataTrainingset' (also consider the case that the dataview has more than one row)
                        for (int i = 0; i < this.dataviewTrainingset.Count; i++)
                        {
                            x_value = this.GetDataviewElement(i, x_name);
                            y_value = this.GetDataviewElement(i, y_name);
                            predict_value = this.GetDataviewElement(i, "__predict");
                            class_value = this.GetDataviewElement(i, "__class"); 

                            // compose tooltip text
                            toolTipText = toolTipText + string.Format("X: {0};  Y: {1:};  Class: {2:};  Predict:  {3:}\n", x_value, y_value, class_value, predict_value);
                        }

                        // display tooltip
                        this.toolTip.ToolTipTitle = "Data point:";
                        this.toolTip.Show(toolTipText, this, x, y);
                    }
                }
                catch
                {
                    MessageBox.Show("An execption occured in function 'MouseToolTip'");
                }
               
            }            
        }

        

        protected override void OnMouseMove(MouseEventArgs e)
        {
            //base.OnMouseMove(e);
                 
            // Show Tooltip when the mouse pointer is pointed at a datapoint in the scatterplot  
            //this.MouseToolTip(e);

        }

        private string GetDataviewElement(int rowNumber, string columnName)
        {          
            // Verify whether the retrieved string is not an empty string
            if (this.dataviewTrainingset[rowNumber][columnName].ToString() == "")
            {
                return "#N/A";
            }
            else
            {
                return this.dataviewTrainingset[rowNumber][columnName].ToString();

            }
        }
        
    }
}
